#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define px first
#define py second

const int N = 200005;
int n, q, c, s, x, cnt, a[N], ft[N];
vector<int> g[N]; bool vis[N];

inline bool ins(int c){return (s >> c) & 1;}
void dfs(int u){
	if(vis[u] || ! ins(a[u])) return; vis[u] = true; cnt ++;
	for(auto v: g[u]) dfs(v);
}
int main(){
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
	scanf("%d %d %d", &n, &q, &c);
	for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
	for(int i = 2; i <= n; i ++){
		scanf("%d", &ft[i]); g[ft[i]].pb(i); g[i].pb(ft[i]);
	}
	while(q --){
		scanf("%d %d", &s, &x); cnt = 0;
		for(int i = 1; i <= n; i ++) vis[i] = false;
		if(! ins(a[x])){puts("1"); continue;}
		dfs(x); printf("%d\n", cnt);
	}
	return 0;
}
